/**
 * @Title: LockCarController.java
 * @Package com.qding.park.api.web.service.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:50:22
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.facade.core.service.common.LockCarCommonFacade;
import com.qding.park.manager.pojo.TbCarlock;

/**
 * @ClassName: LockCarController
 * @Description: 锁车Controller
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:50:22
 *
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/lockCar")
public class LockCarController extends WebBaseController {

	private static final Log _log = LogFactory.getLog(LockCarController.class);

	
	@Autowired
	private  LockCarCommonFacade   lockCarCommonFacade;
	
	
	@Autowired
	private  DemoFacade  demoFacade;

	/**
	 * @Description (创建锁车单)
	 * @param carNum
	 *            车牌
	 * @param parkId
	 *            车场编号
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param userId
	 *            用户id
	 * @return
	 */
	@RequestMapping("/createLockCarOrder")
	public AnswerMsg createLockCarOrder(String carNum, String parkId, String startTime, String endTime, String userId) {
		try {

			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("carNum", carNum);
			vb.getRequiredValidate().put("parkId", parkId);
			vb.getRequiredValidate().put("startTime", startTime);
			vb.getRequiredValidate().put("endTime", endTime);
			vb.getRequiredValidate().put("userId", userId);
			
			vb.getIntValidate().put("parkId", parkId);
			vb.getLongValidate().put("startTime", startTime);
			vb.getLongValidate().put("endTime", endTime);
			RchlValidateTools.validate(vb);
			/**************/
			
			TbCarlock tbCarlock = new TbCarlock();
			tbCarlock.setCarNum(carNum);
			tbCarlock.setParkID(Integer.valueOf(parkId));
			tbCarlock.setStartTime(new Date(Long.valueOf(startTime) * 1000));
			tbCarlock.setEndTime(new Date(Long.valueOf(endTime) * 1000));
			tbCarlock.setBjuserid(userId);
			
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequest(userId)){
				//演示请求
				parkDataResult=demoFacade.createLockCarOrder(tbCarlock);
			}else{
//			    /*根据厂商编号 获取对应的服务层对象*/
//                String supplierId=dicCache.querysupplierIdByid(parkId);
//                LockCarFacade lockCarFacade= this.getServiceObj(SOAServiceEnum.INVITE_SERVICE_NAME, supplierId, LockCarFacade.class);
//                /*根据厂商编号 获取对应的服务层对象*/
			    
				parkDataResult = lockCarCommonFacade.createLockCarOrder(tbCarlock);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (AdapterException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name, "未知Exception",
							"carNum=" + carNum + ",parkId=" + parkId + ",startTime=" + startTime + ",endTime=" + endTime
									+ ",userId=" + userId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}

	/**
	 * @Description (根据订单号解锁)
	 * @param orderId
	 * @return
	 */
	@RequestMapping("/unLockCar")
	public AnswerMsg unLockCar(String orderId) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderId", orderId);

			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequestByOrder(orderId)){
				return AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}else{
				parkDataResult = lockCarCommonFacade.unLockCar(orderId);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}
		catch (AdapterException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name, "未知Exception",
							"orderId=" + orderId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}

	/**
	 * @Description (根据用户id 查询名下所有的锁车单列表)
	 * @param userId
	 * @return
	 */
	@RequestMapping("/lockedCarList")
	public AnswerMsg lockedCarList(String userId) {
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);

			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequest(userId)){
				//演示请求
				parkDataResult=demoFacade.lockedCarList(userId);
			}else{
				parkDataResult = lockCarCommonFacade.lockedCarList(userId);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {
			
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name, "未知Exception",
							"userId=" + userId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}
}
